﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.Collections.Generic;

public partial class Controls_Search_ClubSearch : System.Web.UI.UserControl
{
    private bool isSearch = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitClubType();
            InitDDLProvince();
            if (HttpContext.Current.Request["type"] != null)
            {
                int type = Convert.ToInt32(HttpContext.Current.Request["type"]);
                clubType.SelectedValue = type.ToString();
                isSearch = true;
            }
            if (HttpContext.Current.Request["city"] != null)
            {
                int city = Convert.ToInt32(HttpContext.Current.Request["city"]);
                
                if (city != -1)
                {
                    ddl_Province.SelectedValue = UserRules.Instance.GetProvinceByCityId(city).ProvinceId.ToString();
                    InitDDLCity();
                    ddl_City.SelectedValue = city.ToString();
                }
                isSearch = true;
            }
            if (HttpContext.Current.Request["key"] != null)
            {
                clubName.Text = Server.HtmlDecode(HttpContext.Current.Request["key"]);
                isSearch = true;
            }
        }
        BindPager();
    }

    private void BindPager()
    {
        if (isSearch)
        {
            if (ddl_Province.SelectedValue == "-1")
            {
                Pager.RecordCount = ClubRules.Instance.GetSearchClubCountByKeyAndType(clubName.Text.Trim(), Convert.ToInt32(clubType.SelectedValue));
            }
            else
            {
                Pager.RecordCount = ClubRules.Instance.GetSearchClubCountByKeyAndTypeAndCity(clubName.Text.Trim(), Convert.ToInt32(clubType.SelectedValue), Convert.ToInt32(ddl_City.SelectedValue));
            }
            if (Pager.RecordCount != 0)
            {
                result.Text = "共找到"+Pager.RecordCount+"个俱乐部。";
                result.Visible = true;
            }
        }
    }

    private void InitClubType()
    {
        clubType.DataSource = ClubRules.Instance.GetClubTypes();
        clubType.DataTextField = "texts";
        clubType.DataValueField = "values";
        clubType.DataBind();
        clubType.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("/club/SearchClub.aspx?key="+Server.HtmlEncode(clubName.Text.Trim())+"&type="+clubType.SelectedValue+"&city="+ddl_City.SelectedValue);
        //string p = "";
        //if (clubType.SelectedValue != "-1")
        //{
        //    p += "?type=" + clubType.SelectedValue;
        //}
        //else if (ddl_Province.SelectedValue != "-1")
        //{
        //    p += "?city=" + clubType.SelectedValue;
        //}
        //else
        //{
        //    Response.Redirect("/club/SearchClub.aspx");
        //}
    }

    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        if (isSearch)
        {
            BindData();
        }
    }

    private void BindData()
    {
        List<Club> clubs = new List<Club>();
        if (ddl_Province.SelectedValue == "-1")
        {
            //Pager.RecordCount = ClubRules.Instance.GetSearchClubCountByKeyAndType(clubName.Text.Trim(), Convert.ToInt32(clubType.SelectedValue));
            clubs = ClubRules.Instance.SearchClubByKeyAndType(clubName.Text.Trim(), Convert.ToInt32(clubType.SelectedValue), Pager.CurrentPageIndex, Pager.PageSize);
        }
        else
        {
            //Pager.RecordCount = ClubRules.Instance.GetSearchClubCountByKeyAndTypeAndCity(clubName.Text.Trim(), Convert.ToInt32(clubType.SelectedValue), Convert.ToInt32(ddl_City.SelectedValue));
            clubs = ClubRules.Instance.SearchClubByKeyAndTypeAndCity(clubName.Text.Trim(), Convert.ToInt32(clubType.SelectedValue), Convert.ToInt32(ddl_City.SelectedValue), Pager.CurrentPageIndex, Pager.PageSize);
        }

        if (clubs.Count != 0)
        {
            DataList1.Visible = true;
            noresult.Visible = false;
            DataList1.DataSource = clubs;
            DataList1.DataBind();
        }
        else
        {
            DataList1.Visible = false;
            noresult.Visible = true;
        }
    }

    private void InitDDLProvince()
    {
        ddl_Province.DataSource = UserRules.Instance.GetProvinces();
        ddl_Province.DataTextField = "ProvinceName";
        ddl_Province.DataValueField = "ProvinceId";
        ddl_Province.DataBind();
        ddl_Province.Items.Insert(0,new ListItem("请选择","-1"));
    }

    //protected void HandleDataBind(object sender, EventArgs e)
    //{
    //    HyperLink hy = sender as HyperLink;
    //    hy.NavigateUrl = "/Club/Detail.aspx?clubId=" + hy.NavigateUrl;
    //}

    //protected void HandleImgDataBind(object sender, EventArgs e)
    //{
    //    Image img = sender as Image;
    //    img.ImageUrl = "/upload/club/" + img.ImageUrl;
    //}

    //protected void HandleCityBind(object sender, EventArgs e)
    //{
    //    Label la = sender as Label;
    //    la.Text = UserRules.Instance.GetCityByCityId(Convert.ToInt32(la.Text)).CityName;
    //}

    //protected void HandleAmountBind(object sender, EventArgs e)
    //{
    //    Label la = sender as Label;
    //    la.Text = ClubRules.Instance.GetClubMemberAmountByClubId(Convert.ToInt32(la.Text)).ToString() + "人";
    //}

    //protected void HandleClubHeadBind(object sender, EventArgs e)
    //{
    //    Label la = sender as Label;
    //    la.Text = ClubRules.Instance.GetClubHeadByClubId(Convert.ToInt32(la.Text));
    //}

    //protected void HandleEditBind(object sender, EventArgs e)
    //{
    //    //HyperLink hy = sender as HyperLink;
    //    //hy.NavigateUrl = "/Club/InfoEdit.aspx?clubId=" + hy.NavigateUrl;
    //}

    //protected void DataList1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    Button1_Click(null, null);
    //    DataList1.PageIndex = e.NewPageIndex;
    //    DataList1.DataBind();
    //}
    protected void DataList1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Club club = e.Row.DataItem as Club;
            //if (ClubRules.Instance.GetIsNormalMemberByUserIdAndClubId(club.ClubId, UserRules.Instance.GetCurrentUserId()))
            //{
            //    LinkButton lb = e.Row.FindControl("quitLink") as LinkButton;
            //    lb.Visible = true;
            //    lb.CommandArgument = club.ClubId.ToString();
            //    lb.Click += new EventHandler(lb_Click);
            //}
            (e.Row.FindControl("LabelCity") as Label).Text = club.City.CityName;
            (e.Row.FindControl("ImageLogo") as Image).ImageUrl = "/upload/club/" + club.LogoFileName;
            (e.Row.FindControl("clubLink") as HyperLink).NavigateUrl = "/Club/Detail.aspx?clubId=" + club.ClubId;
            (e.Row.FindControl("clubLink2") as HyperLink).NavigateUrl = "/Club/Detail.aspx?clubId=" + club.ClubId;
            (e.Row.FindControl("clubHead") as Label).Text = ClubRules.Instance.GetClubHeadByClubId(club.ClubId);
            (e.Row.FindControl("peopleAmount") as Label).Text = ClubRules.Instance.GetClubMemberAmountByClubId(club.ClubId).ToString() + "人";
            //(e.Row.FindControl("Des") as Label).Text = HtmlHelper.InterceptSubString(club.Description, 30, true);
        }
    }
    protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_Province.SelectedValue != "-1")
        {
            InitDDLCity();
        }
        else
        {
            ddl_City.Items.Clear();
            ddl_City.Items.Add(new ListItem("请选择", "-1"));
        }
    }

    private void InitDDLCity()
    {
        ddl_City.DataSource = UserRules.Instance.GetCitiesByProvinceId(Convert.ToInt32(ddl_Province.SelectedValue));
        ddl_City.DataTextField = "CityName";
        ddl_City.DataValueField = "CityId";
        ddl_City.DataBind();
    }
}
